#define DEBUG_BUFFERS 0

void dumpBufferfloat(float *d_buffer, int nelements)
{
	//FILE* fp;
	//errno_t err;

	unsigned int bytes = nelements*sizeof(float);
	float *h_buffer = (float *) malloc(bytes);
	cudaMemcpy(h_buffer, d_buffer, bytes, cudaMemcpyDeviceToHost);

	//if((err=fopen_s(&fp,"point.txt", "w"))==0)
	//{

	//int sum = 0;
	printf("dump %u floating-point number:\n", nelements);
	for(int i=0; i<nelements; i++) {
		//if(int(h_buffer[i])<0)
		//fprintf(fp, "%5d: %10f %10f %10f\n", i, h_buffer[6*i+0], h_buffer[6*i+1], h_buffer[6*i+2]);
		//printf("%5d: %27.16e %27.16e %27.16e\n", i, h_buffer[3*i+0], h_buffer[3*i+1], h_buffer[3*i+2]);
		//printf("%5d: %10d, %10d \n", i, int(h_buffer[i])%512, int(h_buffer[i])/512);
		
		
		printf("%5d: %10f\n", i, (h_buffer[i]));
		
		//if(i %4== 0)
		//{			
		//	printf("%5d: %5d %7.3f %7.3f  %7.3f \n", i/4, *((int*)(h_buffer+i)), h_buffer[i+1], h_buffer[i+2], h_buffer[i+3]);
		//	sum+=*((int*)(h_buffer+i));
		//}
	}
	//fclose(fp);
	//}
	//else
	//	printf("Error with open files!\n");
	//fprintf(fp, "	u,			v,			w\n" );	
	//fprintf(fp,"%25.16le %25.16le %25.16le\n", ppoint[0], ppoint[1], ppoint[2]);

	//printf("sum=%d\n", sum);

	printf("\n");
	free(h_buffer);
}


void dumpBuffer(unsigned int *d_buffer, int nelements)
{
	printf("nelements=%d\n", nelements);
	unsigned int bytes = nelements*sizeof(unsigned int);
	unsigned int *h_buffer;
	h_buffer = (unsigned int *) (malloc(bytes));
	cudaMemcpy(h_buffer, d_buffer, bytes, cudaMemcpyDeviceToHost);

	//CUDA_SAFE_CALL(cudaMemcpy(h_buffer, d_buffer, bytes, cudaMemcpyDeviceToHost) );
	printf("%d elements.\n", nelements);
	for(int i=0; i<nelements; i++) {
		//if(int(h_buffer[i])<0)
		//printf("%d: %u, %u, %u \n", i, h_buffer[i]>>12, h_buffer[i]<<20>>21, h_buffer[i]%2);
		//if((h_buffer[i]<<20>>20)>=1144)
		
		printf("%d: %u, %u, %u \n", i, h_buffer[i]>>22, h_buffer[i]<<10>>22, h_buffer[i]<<20>>20);
		//printf("%d: %u, %u\n", i, h_buffer[i]>>10, h_buffer[i]<<22>>22);
		
		//printf("%d: %u\n", i, h_buffer[i]);
		//if (h_buffer[i]==13||h_buffer[i]<0||h_buffer[i]>26)
		//if((h_buffer[i]<<20>>20)>=1636)
		//if(i%50==0)
		if(h_buffer[i]==0)
			//printf("%d: %u\n", i, h_buffer[i]);
			//printf("%d: %u, %u, %u \n", i, h_buffer[i]>>22, h_buffer[i]<<10>>22, h_buffer[i]<<20>>20);
			printf(""); 
	} 

 	printf("\n");

	free(h_buffer); 
}


void dumpBufferFloatFile(float *d_buffer, int nelements, int n)
{
	FILE* fp;
	errno_t err;

	unsigned int bytes = nelements*sizeof(float);
	float *h_buffer = (float *) malloc(bytes);
	cudaMemcpy(h_buffer, d_buffer, bytes, cudaMemcpyDeviceToHost);
	err=fopen_s(&fp,"Out.txt", "w");
	if(err==0)
	{
	fprintf(fp,"Z=[\n");

	int ind=0;
	for(int i=0; i<nelements/n; i++) 
	{
		for(int j=0; j<n; j++)
			if(j%4!=3)
			fprintf(fp, "%7.3f", h_buffer[ind++]);
		fprintf(fp, ";\n");
	} 
	fprintf(fp,"];\n");
	fclose(fp);
	}
	else
		printf("Error with open files!\n");

	free(h_buffer);
}